package gui.ParkingLotManager;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import control.LoginController;
import control.SetRatesController;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Notification;
import vcpClasses.RateSettingRequest;
import vcpClasses.Rates;

import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
 * this class use to Set Rates Screen
 * @author G8
 *
 */
public class SetRatesScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton buttonSetRates;
	private JTextField textFieldNorm1;
	private JTextField textFieldFull;
	private JTextField textFieldAdvance;
	private JTextField textFieldOcc;
	private SetRatesController setRatesController;
	/**
	 *Set Rates Screen Constructor 
	 */
	public SetRatesScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		setRatesController = new SetRatesController();
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Rate Setting", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(QuarterReportScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		buttonSetRates = new JButton("Set Rates");
		buttonSetRates.setBounds(180, 292, 131, 35);
		buttonSetRates.addActionListener(this);
		add(buttonSetRates);
		
		JLabel lblOccasionalOrder = new JLabel("Occasional Order : ");
		lblOccasionalOrder.setBounds(55, 104, 256, 14);
		add(lblOccasionalOrder);
		
		JLabel labelAdvance = new JLabel("In Advance Order : ");
		labelAdvance.setBounds(55, 129, 256, 14);
		add(labelAdvance);
		
		JLabel lblNormSubSingle = new JLabel("Normal Subscription Hours : ");
		lblNormSubSingle.setBounds(55, 154, 256, 14);
		add(lblNormSubSingle);
		
		JLabel labelFullSingle = new JLabel("Full Subscription Hours : ");
		labelFullSingle.setBounds(55, 182, 256, 14);
		add(labelFullSingle);
		
		textFieldNorm1 = new JTextField();
		textFieldNorm1.setBounds(328, 151, 86, 20);
		add(textFieldNorm1);
		textFieldNorm1.setColumns(10);
		
		textFieldFull = new JTextField();
		textFieldFull.setColumns(10);
		textFieldFull.setBounds(328, 179, 86, 20);
		add(textFieldFull);
		
		textFieldAdvance = new JTextField();
		textFieldAdvance.setColumns(10);
		textFieldAdvance.setBounds(328, 126, 86, 20);
		add(textFieldAdvance);
		
		textFieldOcc = new JTextField();
		textFieldOcc.setColumns(10);
		textFieldOcc.setBounds(328, 101, 86, 20);
		add(textFieldOcc);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonSetRates){
			textFieldOcc.setBackground(Color.WHITE);
			textFieldAdvance.setBackground(Color.WHITE);
			textFieldNorm1.setBackground(Color.WHITE);
			textFieldFull.setBackground(Color.WHITE);
			
			if (FieldValidator.checkPrice(textFieldOcc.getText()) && FieldValidator.checkPrice(textFieldAdvance.getText())
					&& FieldValidator.checkNumeric(textFieldNorm1.getText()) && FieldValidator.checkNumeric(textFieldFull.getText()))
			{
				Rates newRates = new Rates(Float.parseFloat(textFieldOcc.getText()), Float.parseFloat(textFieldAdvance.getText()),
						Float.parseFloat(textFieldNorm1.getText()), Float.parseFloat(textFieldFull.getText()));
				RateSettingRequest newRateSettingRequest = new RateSettingRequest("RATES",newRates.toSettingRequest(),newRates,VCP.currentUser.getUserName());
				SystemTypes ratesToUpdate = new SystemTypes(newRateSettingRequest,null);
				SystemTypes ratesResult = setRatesController.UpdateRates(ratesToUpdate);
				if (ratesResult.getSysMode().equals(SystemMode.OK))
					JOptionPane.showMessageDialog(null, ratesResult.getObj().toString(), "Success",
		                    JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null, ratesResult.getObj().toString(), "Error",
		                    JOptionPane.ERROR_MESSAGE);
			}
			else{
				if (!FieldValidator.checkPrice(textFieldOcc.getText())) textFieldOcc.setBackground(Color.RED);
				if (!FieldValidator.checkPrice(textFieldAdvance.getText())) textFieldAdvance.setBackground(Color.RED);
				if (!FieldValidator.checkNumeric(textFieldNorm1.getText())) textFieldNorm1.setBackground(Color.RED);
				if (!FieldValidator.checkNumeric(textFieldFull.getText())) textFieldFull.setBackground(Color.RED);
				JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
